
def create_message(request, msg, msgtype='msg'):
    user_messages = get_message(request)
    user_messages.append(msg)
    request.session['user_messages'] = user_messages
    
def get_message(request):
    user_messages = request.session.get('user_messages', [])
    return user_messages
    
def get_and_delete_message(request):
    user_messages = get_message(request)
    try:
        del request.session['user_messages']
    except KeyError:
        pass
    return {
        'user_messages': user_messages,
    }
    
from realpython.blog.models import *

def create_nav(request):
    path = request.META['PATH_INFO']
    path = path.split("/")
    nav = ''
    if path[1] == 'blog':
        nav = nav + ''' >> <a href="/blog">Blog</a>'''
        if len(path) >= 4:
            if path[2] == 'category':
                category = BlogCategory.get(path[3])
                nav = nav + ''' >> <a href="/blog/category/''' + str(category.key()) + '''">''' + category.category +'''</a>'''
            elif path[2] == 'add':
                nav = nav + ''' >> <a href="/blog/add">Add Blog</a>'''
            else:
                blog = Blog.get(path[2])
                if blog:
                    nav = nav + ''' >> <a href="/blog/category/''' + str(blog.category.key()) + '''">''' + blog.category.category +'''</a>'''
                    nav = nav + ' >> ' + blog.title
    elif path[1] == 'guestbook':
        nav = nav + ''' >> <a href="/guestbook">Guest Book</a>'''
    elif path[1] == 'joke':
        nav = nav + ''' >> <a href="/joke/add">Add Joke</a>'''
    elif path[1] == 'account':
        if len(path) >= 4:
            if path[2] == 'login':
                nav = nav + ''' >> <a href="/account/login">Login</a>'''
            elif path[2] == 'register':
                nav = nav + ''' >> <a href="/account/register">Register</a>'''
                
    return {
        'nav':nav,
    }
    
    

from realpython.index.models import *
def get_counter(request):
    counter = Counter.all().get()
    return {'pvcount': counter.pvcount, 'ipcount': counter.ipcount}

def get_vister(request):
    visiters = Visiters.all().order('-created').fetch(10)
    for visiter in visiters:
        if visiter.gmail:
            visiter.display = visiter.gmail
        elif visiter.username:
            visiter.display = visiter.username
        else:
            ip = visiter.ip.split(".")
            visiter.display = ip[0]+'.'+ip[1]+'.'+ip[2]+'.*'
    return {'visiters': visiters}




